package com.diyunapp.xilinapp.login;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ForPassPhoneActivity extends StatusBarFragmentActivity {

    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.fpassname_et)
    EditText fpassnameEt;
    @Bind(R.id.user_email_et)
    EditText userEmailEt;
    @Bind(R.id.login_tv)
    TextView loginTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_for_pass_phone);
        ButterKnife.bind(this);
        View headView = findViewById(R.id.forget_menubar_inc);
        super.MenuWidget(headView);
    }


    @OnClick({R.id.go_back, R.id.login_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.go_back:
                finish();
                break;
            case R.id.login_tv:
                if(fpassnameEt.getText().toString().trim()==""){
                    toast("登录账户不能为空");
                }else if (userEmailEt.getText().toString().trim()==""){
                    toast("邮箱不能为空");
                }
                else{
                    //找回密码

                    find();

                }
                break;
        }
    }





    public void find() {
        String username=fpassnameEt.getText().toString().trim();
        String phone=userEmailEt.getText().toString().trim();
        String url= MainActivity.url+"act=goods&op=findmob_password&mobile="+phone+"&username="+username;
        Log.i("zhangxiaoxidebug", "url="+url);
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST,url,
                new RequestCallBack<String>()
                {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading)
                    {

                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo)
                    {
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.getInt("code");
                            String data = resultObject.getString("data");
                            if(result==200)
                            {
                                //dialog
                                toast(data);
//                                toast("邮件已发送至手机");
                                setResult(1);
                                finish();
                            }else{

                                toast(data);
                            }

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();

                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg)
                    {
                        toast("请求失败");
                    }
                });
    }
}
